/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.threads.client.inGame;
import core.messages.client.inGame.Msg_Response_Yes;
import core.threads.DataBase;
import core.threads.client.Client_Public_Data;
import core.threads.client.Client_Public_MsgBox;
import core.threads.client.Client_Public_Thread;
import cstAndEnum.SKILL_TYPE;
import java.util.ArrayList;
import java.util.List;
import models.Card.Card;
import models.Card.CardSpell;
import models.CastInfo;
import models.Player;
import models.Skill.Skill;
import myTest.MYTest;

/**
 *
 * @author Invince
 */
public class 舍弃版本_T_Client_Response_Copy  extends Thread{
/*----------Attributs-------------------*/
//    protected boolean stop;
//    protected boolean suspend;

    protected CastInfo previousCastInfo;
/*----------Public functions------------*/
    @Override
    public void run() {
        /**-----Init-----**/
        String name;
        synchronized(Client_Public_Data.semaMyInfo){
            name=Client_Public_Data.myInfo.getName();
        }
        boolean isMyTurn;
        synchronized(Client_Public_Data.semaIsMyTurn){
            isMyTurn=Client_Public_Data.isMyTurn;
        }
        if(!isMyTurn){
            Client_Public_Thread.tClientActiveSkill.resumeT();
        }
        /**-----Main----**/
        boolean responsable=false;
        CastInfo castInfo = null;
        while(!responsable){
            while(Client_Public_MsgBox.msgBox_Client_Response.isEmpty()){}
            Msg_Response_Yes mRY=(Msg_Response_Yes)Client_Public_MsgBox.msgBox_Client_Response.poll();//????????这里没考虑NO的情况
            castInfo=mRY.getCastInfo();
            responsable=isResponsable(castInfo);
            if(!responsable){
                MYTest.println("You can't respond with this card or Skill! Rechoose one.");
            }
        }
        int idCard=castInfo.getCard().getId();
        int idSkill=castInfo.getSkill().getId();
        int idSource=castInfo.getSource().getId();
        int idTarget=castInfo.getTargetPlayers().get(0).getId();//只有一个target
        SKILL_TYPE skillType=castInfo.getSkillType();
        Msg_Response_Yes msgCardResponse=new Msg_Response_Yes(name, "Server", idCard,idSkill, idSource, idTarget,skillType);
        Client_Public_MsgBox.msgBox_Client_Send_Msg.offer(msgCardResponse);
        /**----End-----**/
        if(!isMyTurn){
            Client_Public_Thread.tClientActiveSkill.suspendT();
        }
    }

//    public void stopT(){
//        synchronized(this){
//            this.stop=true;
//        }
//    }
//
//    public void suspendT(){
//        synchronized(this){
//            this.suspend=true;
//        }
//    }
//
//    public void resumeT(){
//        synchronized(this){
//            this.suspend=false;
//        }
//    }
/*----------Private functions-----------*/
    private boolean isResponsable(CastInfo castInfo){//？？？？？？这个还未完成
        if(this.previousCastInfo.getCard() instanceof CardSpell){
            if(castInfo.getSkill().getName().equals("无懈可击")){
                return true;
            }
        }
        if(this.previousCastInfo.getSkill().getName().equals("杀")){
            if(castInfo.getSkill().getName().equals("闪")){
                return true;
            }
        }
        if(this.previousCastInfo.getSkill().getName().equals("南蛮入侵")){//注意无懈可击一开始判断了
            if(castInfo.getSkill().getName().equals("杀")){
                return true;
            }
        }
        if(this.previousCastInfo.getSkill().getName().equals("万箭齐发")){
            if(castInfo.getSkill().getName().equals("闪")){
                return true;
            }
        }
        return false;
    }
/*----------Constructor-----------------*/
    public 舍弃版本_T_Client_Response_Copy(CastInfo previousCastInfo) {
        synchronized(this){
            this.previousCastInfo=previousCastInfo;
//            this.stop=false;
//            this.suspend=false;
        }
    }

    public 舍弃版本_T_Client_Response_Copy(int idSkill, int idSource, int[] idTargets, int idCard, SKILL_TYPE skillType) {
        synchronized(this){
            Skill skill=DataBase.findSkillByID(idSkill);
            Card card=DataBase.findCardByID(idCard);
            Player source;
            List<Player> targets=new ArrayList<Player>();
            synchronized(Client_Public_Data.semaGameInfoClient){
                source=Client_Public_Data.gameInfoClient.getListPlayer().findPlayerById(idSource);
                for(int i=0;i<idTargets.length;i++){
                    targets.add(Client_Public_Data.gameInfoClient.getListPlayer().findPlayerById(idTargets[i]));
                }
            }
            this.previousCastInfo=new CastInfo(source, targets, skill, card,skillType);
//            this.stop=false;
//            this.suspend=false;
        }
    }
/*----------Gets and sets---------------*/
}
